<template>
  <div>
    <van-nav-bar
      title="招聘信息录入"
      left-arrow
      left-text="首页"
      @click-left="handleClickLeft"
    />

    <van-notice-bar
      :text="'您录入招聘信息，我帮您招人。填写越详细，招人越容易！'"
      left-icon="volume-o"
      :scrollable="false"
      wrapable
      color="#1989fa"
      background="#ecf9ff"
    />
    <info-form
      @getJobContent="toPublishJob"
      :btnShow="true"
      :keyword="keyword"
      :jobDetailInfo="jobDetailObj"
      :actionType="actionType"
    />
  </div>
</template>

<script>
import {Dialog, Toast} from "vant";
import infoForm from "./infoForm";
import {base_url_path} from "../../constants/comUtils";

export default {
  name: "info",
  data(){
    return {
      jobId: '',
      // 注意：这个定义的值是不一样的。
      job_id: '',
      partnerId: '',
      jobDetailObj: {},
      base_url_path,
      actionType: 'addInfo',
      keyword: 'publishJob',
    }
  },
  components: {
    infoForm,
  },

  created() {
    if (this.$route.query.ids) {
      // 合作者的id
      this.partnerId = this.$route.query.ids;

      if (this.$route.query.jobId && this.$route.query.sId) {
        // 收集表的两个jobid
        this.jobId = this.$route.query.jobId;
        this.job_id = this.$route.query.sId
        // 若有jobId，则要修改收集表
        this.actionType = 'updateInfo';
      };

    } else {
      Dialog.alert({
        title: '异常提示',
        message: '此链接无效！可能无法帮您招人。请联系您的合作者，让他重新发给您链接。',
        messageAlign: 'left',
      }).then(() => {

      })
    }
  },


  mounted() {

    if (this.jobId){
      this._getJobDetailObj();
    };

    const val = localStorage.getItem('draft_publish_info');
    // 只有是新增才能保存草稿
    if (val && this.actionType === 'addInfo') {
      this.jobDetailObj = JSON.parse(val)
    };

  },

  methods: {

    async _getJobDetailObj() {
      const res = await this.$api.others.reqJobSheetDetailObj(this.jobId);
      if (res.code === 200) {
        this.jobDetailObj = res.data
      }
    },

    handleClickLeft(){
      // Toast('已保存为草稿');
      let allCertificate = window.localStorage.getItem('allCertificate');
      if(allCertificate){
        this.$router.back();
      } else {
        Dialog.confirm({
          title: '温馨提示',
          message: '您确定关闭吗？点击确定，可前往软件的首页进行体验。',
          messageAlign: 'left',
        }).then(() => {
          // on confirm
          this.$router.push('/')
        }).catch(() => {
          // on cancel
        });
      };
    },

    handleClickRight(jobItem){
      // 跳转至详情页
      this.$router.push({
        path: '/jobDetail',
        query: {
          id: jobItem.ids,
          sign_state: jobItem.sign_state,
          sign_remark: jobItem.sign_remark,
          // 需要查看自己的招聘信息的备注信息
          keyword: 'jobSheet',
        }
      })
    },



    // 发布或者修改
    async toPublishJob(value){
      // console.log('收集表提交', value);
      const newValueObj = {
        ...value,
        // 每个链接都要分享者的id
        partner_id: this.partnerId,
        // 有job_id说明就是修改，那就要传值的
        ...(this.job_id && { job_id: this.job_id }),
      };

      const res = await this.$api.others.addJobSheetInfo(newValueObj);
      if (res.code === 200) {
        Dialog.confirm({
          title: '提交成功',
          message: '点击确定，可前往我们的软件首页进行体验。',
          messageAlign: 'left',
        }).then(() => {
          // on confirm
          location. reload()
          this.$router.push('/')
        }).catch(() => {
          // on cancel
          if (this.jobId){
            location. reload()
            this._getJobDetailObj();
          }
        });
      }
    },
  },
}
</script>

<style lang="scss" scoped>
</style>
